from PySide6.QtWidgets import QDialog,QMessageBox
from sqlalchemy.orm import Session
from settings.publicvar import engine
from views.person.model.person import PersonBaseInfo

class OperationView(QDialog,Ui_Dialog):
    def __init__(self,is_state,data):
        super().__init__()
        self.setupUi(self)
        self.name_id = False
        self.if_status(is_state)
        self.setFormStructure(data)
        self.savebtn.clicked.connect(self.saveFun)
        self.changebtn.clicked.connect(self.changeFun)
        self.deletebtn.clicked.connect(self.deleteFun)
        self.resetbtn.clicked.connect(self.resetFun)

        self.name.editingFinished.connect(self.nameFinishedFun)

    # 状态判断
    def if_status(self,is_status):
        self.name.setDisabled(True)
        if is_status == 'add':
            self.IDcard.setText('身份证号')
            self.name.setDisabled(False)
            self.deletebtn.hide()
            self.changebtn.hide()
        elif is_status == 'change':
            self.titlelabel.setText('修改银行卡信息')
            self.savebtn.hide()
            self.deletebtn.hide()
        elif is_status == 'delete':
            self.titlelabel.setText('删除银行卡信息')
            self.savebtn.hide()
            self.changebtn.hide()
            self.resetbtn.hide()
        else:
            self.titlelabel.setText('查看银行卡信息')
            self.savebtn.hide()
            self.changebtn.hide()
            self.resetbtn.hide()
            self.deletebtn.hide()

    # 表单加载
    def setFormStructure(self,data):
        if data:
            self.idx.setText(data[0])
            self.name.setText(data[1])
            self.bankname.setText(data[2])
            self.bankcard.setText(data[3])
            self.openbank.setText(data[4])
            self.mark.setText(data[5])

    # 获取表单数据
    def getFormData(self):
        idx = self.idx.text().strip()
        bankname = self.bankname.text().strip()
        bankcard = self.bankcard.text().strip()
        openbank = self.openbank.text().strip()
        mark = self.mark.text().strip()

        data = {'bankname':bankname,'bankcard':bankcard,'opencard':openbank,'mark':mark}
        if idx is None:
            return [False,data]
        else:
            return [idx,data]

    # 保存数据
    def saveFun(self):
        data = self.getFormData()
        data[1]['name_id'] = self.name_id
        with Session(engine) as session,session.begin():
            bankcard = XXX(**data[1])
            session.add(bankcard)
            QMessageBox.information(None,'成功','恭喜添加成功！')
        self.close()

    def changeFun(self):
        data = self.getFormData()
        with Session(engine) as sessin,sessin.begin():
            sessin.query(XXX).filter(XXX.id == data[0]).update(data[1])
            QMessageBox.information(None, '成功', '恭喜修改成功！')
        self.close()
        print(data,'data')

    def deleteFun(self):
        data = self.getFormData()
        with Session(engine) as session,session.begin():
            with Session(engine) as sessin, sessin.begin():
                sessin.query(XXX).filter(XXX.id == data[0]).delete()
                QMessageBox.information(None, '成功', '恭喜删除成功！')
            self.close()

    def resetFun(self):
        self.bankname.setText('')
        self.bankcard.setText('')
        self.openbank.setText('')
        self.mark.setText('')

    def nameFinishedFun(self):
        IDcard = self.name.text().strip()
        with Session(engine) as session,session.begin():
            try:
                personbase = session.query(PersonBaseInfo).filter(PersonBaseInfo.IDcard == IDcard).one()
                if not personbase.personothers:
                    self.name.setText(personbase.name)
                    self.name_id = personbase.id
                    self.IDcard.setText('姓   名')
                else:
                    QMessageBox.critical(None, '失败', '数据已经增加了，不用添加！')
                    self.close()
            except:
                QMessageBox.critical(None,'失败','没有找到基本信息，请选增加基本信息,请检查身份证是否输错！')